package stage1.operation.module3.student;

import stage1.operation.module3.student.exception.AgeException;
import stage1.operation.module3.student.exception.StudentIdException;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Board {
    public static void main(String[] args) throws IOException {
        try {
            getStudentList();
            start();
        } catch (AgeException e) {
            e.printStackTrace();
        } catch (StudentIdException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }catch (Exception e) {
            System.out.println("请安正确规则操作！");
            e.printStackTrace();
        }finally {
            saveStudentList();
        }
    }

    private static void start() throws StudentIdException, AgeException, IOException {
        System.out.println("********学生信息管理系统********");
        System.out.println("1->查看所有学生");
        System.out.println("2->查询学生");
        System.out.println("3->增加学生");
        System.out.println("4->修改学生");
        System.out.println("5->删除学生");
        System.out.println("6->退出系统");
        String key = new Scanner(System.in).nextLine();
        switch (key) {
            case "1":
                traverse();
                break;
            case "2":
                queryStudents();
                break;
            case "3":
                add();
                break;
            case "4":
                modify();
                break;
            case "5":
                delete();
                break;
            case "6":
                saveStudentList();
                System.out.println("退出系统");
                System.exit(0);
                break;
            default:
                System.out.println("请选择要进行的操作");
                break;
        }
        start();
    }

    /**
     * 遍历所有学生信息
     */
    private static void traverse() {
        List<Student> list = StudentList.getStudentList();
        if (list.isEmpty()) {
            System.out.println("暂无学生信息，请重试");
            return;
        }
        for (Student student : list) {
            System.out.println("学生：" + student.getName() + " 的学号是：" + student.getStudentID() + " 年龄是：" + student.getAge());
        }
    }

    /**
     * 查询学生信息
     */
    private static void queryStudents() throws StudentIdException {
        Student student = getStudent(getStudentId());
        if (student == null) {
            System.out.println("该学生信息不存在，请重试");
            return;
        }
        System.out.println("学生：" + student.getName() + " 的学号是：" + student.getStudentID() + " 年龄是：" + student.getAge());
    }

    /**
     * 获取学生信息
     *
     * @param studentId 学生学号
     * @return
     */
    private static Student getStudent(int studentId) {
        Student student = null;
        ArrayList<Student> list = StudentList.getStudentList();
        for (Student stu : list) {
            if (stu.getStudentID() == studentId) {
                student = stu;
            }
        }

        return student;
    }

    /**
     * 添加学生信息
     */
    public static void add() throws StudentIdException, AgeException {
        int studentId = getStudentId();
        String name = getName();
        int age = getAge();
        ArrayList<Student> list = StudentList.getStudentList();
        Student student = getStudent(studentId);
        if (student != null) {
            System.out.println("该学号已存在，请重试");
            return;
        }

        Student s = new Student(studentId, name, age);
        list.add(s);
        StudentList.setStudentList(list);
        System.out.println("添加成功");
    }

    /**
     * 修改学生信息
     */
    public static void modify() throws AgeException, StudentIdException {
        int studentId = getStudentId();
        String name = getName();
        int age = getAge();
        List<Student> list = StudentList.getStudentList();
        Student student = getStudent(studentId);
        if (student == null) {
            System.out.println("该学生信息不存在，请重试");
            return;
        }

        student.setName(name);
        student.setAge(age);
        list.set(list.indexOf(student), student);
        System.out.println("修改成功");
    }

    /**
     * 删除学生信息
     */
    public static void delete() throws StudentIdException {
        int studentId = getStudentId();
        List<Student> list = StudentList.getStudentList();
        Student student = getStudent(studentId);
        if (student == null) {
            System.out.println("该学生信息不存在，请重试");
            return;
        }

        list.remove(list.indexOf(student));
        System.out.println("删除成功");
    }

    /**
     * 获取学生学号
     * @return
     * @throws StudentIdException
     */
    private static int getStudentId() throws StudentIdException {
        System.out.println("请输入学生学号：");
        int studentId = new Scanner(System.in).nextInt();
        // 实在不知道咋加这个异常，强行加的。😅
        if (studentId < 10000 || studentId > 99999) {
            throw new StudentIdException("请输入正确的学号！");

        }

        return studentId;
    }

    /**
     * 获取年龄
     * @return
     * @throws AgeException
     */
    private static int getAge() throws AgeException {
        System.out.println("请输入学生年龄");
        int age = new Scanner(System.in).nextInt();
        if (age > 120 || age < 0) {
            throw new AgeException("请输入合理的年龄");
        }

        return age;
    }

    /**
     * 获取姓名
     * @return
     */
    private static String getName() {
        System.out.println("请输入学生姓名");

        return new Scanner(System.in).nextLine();
    }

    /**
     * 开始时从文件读取学生信息
     * @throws IOException
     */
    private static void getStudentList() throws IOException {
        File file = new File("student_list");
        if (!file.exists())
        {
            StudentList.setStudentList(new ArrayList());
            return;
        }
        BufferedReader br = new BufferedReader(new FileReader("student_list"));
        ArrayList<Student> studentList = new ArrayList<Student>();
        String line;
        while((line = br.readLine()) != null){
            String[] stirs = line.split(",");
            Student student = new Student();
            student.setStudentID(Integer.parseInt(stirs[0]));
            student.setName(stirs[1]);
            student.setAge(Integer.parseInt(stirs[2]));
            studentList.add(student);
        }
        br.close();
        StudentList.setStudentList(studentList);
    }

    /**
     * 退出时保存学生信息
     * @throws IOException
     */
    private static void saveStudentList() throws IOException {
        List<Student> studentList = StudentList.getStudentList();
        BufferedWriter bw = new BufferedWriter(new FileWriter("student_list"));
        for (Student student : studentList) {
            System.out.println(student.toString());
            bw.write(student.getStudentID()+","+student.getName()+","+student.getAge());
            bw.newLine();
        }
        bw.close();
    }

}
